package com.mzkj.wanjulaile.mobile.api.modules.toy.service;

import com.mzkj.common.utils.Collections3;
import com.mzkj.wanjulaile.mobile.api.modules.toy.entity.Image;
import com.mzkj.wanjulaile.mobile.api.modules.toy.entity.Toy;
import com.mzkj.wanjulaile.mobile.api.modules.toy.mapper.ImageMapper;
import com.mzkj.wanjulaile.mobile.api.modules.toy.mapper.ToyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional(readOnly = true)
public class ToyServiceImpl implements ToyService {
    @Autowired
    private ImageMapper imageMapper;
    @Autowired
    private ToyMapper toyMapper;

    public Toy get(String id) {
        return toyMapper.get(id);
    }

    public List<Toy> findAllList(Toy toy) {
        return toyMapper.findAllList(toy);
    }

    /**
     * 获取新品数据
     */
    public List<Toy> getNewProduct(int pageSize) {
        if (pageSize == 0 || pageSize > 100) {
            pageSize = 100;
        }
        return toyMapper.getNewProduct(pageSize);
    }


    //获取产品列表
    public List<Toy> getHomeToyList(Toy toy) {
        return toyMapper.findToyList(toy);
    }


    //根据ID查看产品详情
    public Toy getById(Toy toy) {
        return toyMapper.getById(toy);
    }


    /**
     * 玩具ids查询
     *
     * @param ids
     * @return
     */
    public List<Toy> getByIds(List<String> ids) {
        List<Toy> toyList = toyMapper.getByIds(ids);
        if (!Collections3.isEmpty(toyList)) {
            for (Toy toy : toyList) {
                setImage(toy);
            }
        }
        return toyList;
    }

    private void setImage(Toy toy) {
        Image image = new Image();
        image.setToy(toy);
        toy.setImages(imageMapper.getListByToyId(image));
    }

    /**
     * 微信端查询收藏预约数据
     *
     * @param toy
     * @return
     */
    public List<Toy> getListByType(Toy toy) {
        return toyMapper.getListByType(toy);
    }


    @Transactional(readOnly = false)
    public Toy getToyByCode(Toy toy) {
        return toyMapper.getToyByCode(toy);
    }

    /**
     * 获取所有上架商品的总数
     *
     * @return
     */
    public int getAllCount(Toy toy) {
        return toyMapper.getAllCount(toy);
    }

}